#include <gflags/gflags.h>

#include "transmite_server.hpp"

DEFINE_bool(debug, true, "默认开启debug模式");
DEFINE_string(logPath, "", "发布模式下，日志路径");
DEFINE_int32(logLevel, 0, "发布模式下，日志级别");

DEFINE_string(transmiteHost, "http://127.0.0.1:2379", "etcd的IP地址");
DEFINE_string(baseService, "/service", "服务监控根目录");
DEFINE_string(userService, "/service/user_service", "消息转发子服务名称");

DEFINE_string(instanceName, "/transmite_service/instance", "当前实例名称");
DEFINE_string(accessHost, "127.0.0.1:10004", "当前实例的外部访问地址");

DEFINE_string(mysql_host, "127.0.0.1", "Mysql服务器访问地址");
DEFINE_string(mysql_user, "root", "Mysql服务器访问用户名");
DEFINE_string(mysql_pswd, "123456", "Mysql服务器访问密码");
DEFINE_string(mysql_db, "im", "Mysql默认库名称");
DEFINE_string(mysql_cset, "utf8", "Mysql客户端字符集");
DEFINE_int32(mysql_port, 0, "Mysql服务器访问端口");
DEFINE_int32(mysql_pool_count, 4, "Mysql连接池最大连接数量");

DEFINE_string(mq_user, "root", "消息队列服务器访问用户名");
DEFINE_string(mq_pswd, "123456", "消息队列服务器访问密码");
DEFINE_string(mq_host, "127.0.0.1:5672", "消息队列服务器访问地址");
DEFINE_string(mq_msg_exchange, "msg_exchange", "持久化消息的发布交换机名称");
DEFINE_string(mq_msg_queue, "msg_queue", "持久化消息的发布队列名称");
DEFINE_string(mq_msg_binding_key, "msg_queue", "持久化消息的发布队列名称");

DEFINE_int32(listenPort, 10004, "Rpc服务器监听端口");
DEFINE_int32(rpcTimeout, -1, "Rpc调用超时时间");
DEFINE_int32(rpcThreads, 10, "Rpc的IO线程数量");

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    im::initLogger(FLAGS_debug, FLAGS_logPath, FLAGS_logLevel);

    im::MsgTransmitServerBuild mts_build;
    mts_build.init_discover_client(FLAGS_transmiteHost, FLAGS_baseService, FLAGS_userService);
    mts_build.init_register_client(FLAGS_transmiteHost, FLAGS_baseService + FLAGS_instanceName, FLAGS_accessHost);
    mts_build.init_mysql_client(FLAGS_mysql_user, FLAGS_mysql_pswd,FLAGS_mysql_host, FLAGS_mysql_db, 
                            FLAGS_mysql_cset, FLAGS_mysql_port, FLAGS_mysql_pool_count);
    mts_build.init_MQ_client(FLAGS_mq_user, FLAGS_mq_pswd, FLAGS_mq_host, 
                        FLAGS_mq_msg_exchange, FLAGS_mq_msg_queue, FLAGS_mq_msg_binding_key);
    mts_build.init_rpc_server(FLAGS_listenPort, FLAGS_rpcTimeout, FLAGS_rpcThreads);
    im::MsgTransmitServer::ptr ssr = mts_build.build();
    ssr->start();
    return 0;
}